#include <iostream>
#include <algorithm>
using namespace std;

struct room
{
    int start;
    int end;
};

void swap(int &a, int &b)
{
    int t;
    t = a;
    a = b;
    b = t;
    return;
}

bool cmp(struct room a, struct room b)
{
    if (a.end != b.end)
    {
        return a.end < b.end;
    }
    else
    {
        return a.start < b.start;
    }
}

int main()
{
    struct room moves[210];
    int useds = 0;
    int T, N;
    int time;
    int end_room;
    bool used[200];
    bool moved;
    int i;
    cin >> T;
    while (T--)
    {
        cin >> N;
        for (i = 0; i < N; i++)
        {
            cin >> moves[i].start >> moves[i].end;
            if (moves[i].start > moves[i].end)
            {
                swap(moves[i].start, moves[i].end);
            }
            if (!(moves[i].start & 1))
            {
                moves[i].start -= 1;
            }
            if (moves[i].end & 1)
            {
                moves[i].end += 1;
            }
        }
        sort(moves, moves + N, cmp);
        for (i = 0; i < N; i++)
        {
            used[i] = true;
        }
        time = 0;
        useds = 0;
        while (useds < N)
        {
            moved = false;
            end_room = 0;
            for (i = 0; i < N; i++)
            {
                if (moves[i].start > end_room && used[i])
                {
                    used[i] = false;
                    useds += 1;
                    moved = true;
                    end_room = moves[i].end;
                }
            }
            if(moved)
            {
                time += 10;
            }
        }
        cout << time << "\n";
    }
}